'use strict';

    angular.module('app')
        .controller('detailController', ['$scope', '$rootScope', '$state', '$timeout', 
                                             function($scope, $rootScope, $state, $timeout) {
        var title = "广告位详情";
        var id = $state.params.id;
        activate(id);
        $scope.title = $rootScope.title = title;
        
        $scope.loading = true;

        $scope.typeList = [{typeId : "2", typeName : "是"}, {typeId : "1", typeName : "否"}];

        //初始化验证
        $scope.submit = function(){
            $scope.loading = true;
            if($scope.myImage != '' && $scope.myImage != null && $scope.myImage != 'undefined') {
            	$.ajax({
    				url : $scope.app.project_name + '/upload/imageData',
    				data: {fileData:$scope.myCroppedImage}
    			}).then(function(result){
                    if(result && result.httpCode ==200){//成功
                        $scope.record['avatar'] =result.imgName[0];
                        saveData();
                    }else if(result && result.httpCode ==400){
                        saveData();
                    }
                });
            } else {
            	$scope.record['avatar'] = $scope.myCroppedImage;
            	saveData();
            }
        };
         $scope.approve = function(id, state) {
             var remark=$('#remark').val();
             if(state==3){
                 $scope.alertMessage({
                     text: "确定做此操作吗？", //内容
                     btnCancel: true, //取消按钮
                     btnConfirm: true //确认按钮
                 }).then(function () {
                     $.ajax({
                         url: $scope.app.project_name + '/adSlot/update/approve',
                         data: {'id': id, 'state': state,'remark':'审核通过'}
                     }).then(function (result) {
                         $scope.loading = false;
                         if (result.httpCode == 200) {
                             $scope.alertModal({"content": "操作成功"});
                             $state.go('main.biz.adSlot.list');
                         } else {
                             $scope.msg = result.msg;
                         }
                         $scope.$apply();
                     });
                 });
             }else {
                 if(remark==null||remark==''){
                     $scope.alertModal({"content": "请填写拒绝理由！"});

                 }

                 $.ajax({
                     url: $scope.app.project_name + '/adSlot/update/approve',
                     data: {'id': id, 'state': state,'remark':remark}
                 }).then(function (result) {
                     $scope.loading = false;
                     if (result.httpCode == 200) {
                         $scope.alertModal({"content": "操作成功"});
                         $state.go('main.biz.adSlot.list');
                     } else {
                         $scope.alertModal({"content": "操作失败"});
                         $state.go('main.biz.adSlot.list');
                         $scope.msg = result.msg;
                     }
                     $scope.$apply();
                 });
             }
         }

        function saveData(){
            var m = $scope.record;
            if(m){
                $scope.isDisabled = true;//提交disabled
                $.ajax({
    				url : $scope.record.id ? $scope.app.project_name+'/user/update' : $scope.app.project_name+'/user/add',
    				data: m
    			}).then(callback);
            }
            function callback(result){
                if(result.httpCode ==200){//成功
                    $timeout(function(){
                        $state.go('main.sys.user.list');
                    },2000);
                }else{
                    $scope.isDisabled = false;
                }
                $scope.loading = false;
            }
        }

        // 初始化页面
        function activate(id) {
	        $scope.loading = true;
        	$.ajax({
				url : $scope.app.project_name+'/adSlot/read/detail',
				data: {'id': id}
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.record = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
        }
         $scope.addPosArr={
             "pc":[0,1,2,3,4,5,6,7,8],
             "mobile":[0,1,2]
         }
         $scope.preview = function(){
             $scope.firstApply = true;
         }

         $scope.updatePrice=function (id) {
             var adsoltPrice = $("#adsoltPrice").val();
             // if(Number($scope.record.adslotPrice)<Number(adsoltPrice)){
             //     $scope.alertModal({"content": "服务价格不能高于广告位价格"});
             //     return;
             // }
             if(Number(adsoltPrice)>0){
                 $.ajax({
                     url : $scope.app.project_name+'/adSlot/update/updateAdsoltPrice',
                     data: {'id': id,adsoltPrice:adsoltPrice}
                 }).then(function(result) {
                     $scope.loading = false;
                     if (result.httpCode == 200) {
                         $scope.alertModal({"content": "操作成功"});
                         window.location.reload()
                     } else {
                         $scope.alertModal({"content": "操作失败"});
                     }
                     $scope.$apply();
                 });
             }else {
                 $scope.alertModal({"content": "价格不对"});
             }

         }

        //修改零元购类型
         $scope.updateZero=function (id,type) {
             $.ajax({
                 url : $scope.app.project_name+'/adSlot/update/updateAdsoltZeroType',
                 data: {'id': id,'deliveryType':type}
             }).then(function(result) {
                 $scope.loading = false;
                 if (result.httpCode == 200) {
                     $scope.alertModal({"content": "操作成功"});
                     window.location.reload()
                 } else {
                     $scope.alertModal({"content": "操作失败"});
                 }
                 $scope.$apply();
             });
         }

    }]);